﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PaymentHelper.Core.MultiLanguage
{
    public class LanguageContainer
    {
        private IList<LanguagePackage> _repository;

        public LanguagePackage this[string code]
        {
            get
            {
                return GetLanguagePackageByCode(code);
            }
            set
            {
                AddLanguagePackage(code,value);
            }
        }

        public LanguagePackage GetDefaultLanguagePackage()
        {
            return GetLanguagePackageByCode(LanguageManager.DefaultLanguageCode);
        }

        private LanguagePackage GetLanguagePackageByCode(string code)
        {
            if (_repository == null || _repository.Count == 0)
                return null;
            return _repository.SingleOrDefault(lp => lp.Code.Equals(code, StringComparison.InvariantCultureIgnoreCase));
        }

        private void AddLanguagePackage(string code, LanguagePackage languagePackage, bool overrideIfExist=true)
        {
            if (_repository == null)
                _repository = new List<LanguagePackage>();

            if (!code.Equals(languagePackage.Code, StringComparison.InvariantCultureIgnoreCase))
                return;

            var lp = GetLanguagePackageByCode(code);

            if (lp != null)
            {
                lp = languagePackage;
            }
            else
            {
                _repository.Add(languagePackage);
            }
        }
    }
}
